<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch"
             label-width="68px">
      <el-form-item label="采购单号" prop="nos">
        <el-input v-model="queryParams.nos" placeholder="请输入订单编号"
                  clearable @keyup.enter.native="handleQuery"/>
      </el-form-item>
      <el-form-item label="采购日期" prop="ddDate">
        <el-date-picker v-model="dateRange"
                        style="display: flex;"
                        value-format="yyyy-MM-dd"
                        type="daterange"
                        range-separator="-"
                        start-placeholder="开始日期"
                        end-placeholder="结束日期"
        ></el-date-picker>
      </el-form-item>
      <el-form-item label="供应商" prop="supplierName">
        <el-input v-model="queryParams.supplierName" placeholder="请输供应商"
                  clearable @keyup.enter.native="handleQuery"/>
      </el-form-item>
      <el-form-item label="采购类型" prop="goodsType">
        <el-select v-model="queryParams.goodsType" placeholder="请选择采购类型">
          <el-option
            v-for="dict in dict.type.goods_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
        <el-form-item label="付款状态" prop="payApplyState">
          <el-select v-model="queryParams.payApplyState" placeholder="请选择付款状态">
            <el-option
              v-for="dict in dict.type.buy_pay_status"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAddMerge">新增铁件采购
        </el-button>
      </el-form-item>
    </el-form>
    <el-table border v-loading="loading" :data="dataList" @selection-change="handleSelectionChange" ref="table"
              :max-height="tableMaxHeight" @sort-change="handleSortChange">
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column sortable label="采购编号" align="center" prop="nos"/>
      <el-table-column sortable label="供应商名称" align="center" prop="supplierName" show-overflow-tooltip
                       width="120"/>
      <el-table-column sortable label="采购日期" align="center" prop="ddDate" width="120">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.ddDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="订单金额" align="center" prop="money"/>
      <el-table-column sortable align="center" label="采购类别" prop="ywType">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.caigou_type" :value="scope.row.ywType"/>
        </template>
      </el-table-column>
      <el-table-column sortable align="center" label="采购类型" prop="goodsType">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.goods_type" :value="scope.row.goodsType"/>
        </template>
      </el-table-column>
      <el-table-column sortable label="申请付款金额" align="center" prop="payApplyTotalMoney"/>
      <el-table-column sortable label="实际到货金额" align="center" prop="actualArrivalTotalMoney"/>
      <el-table-column sortable align="center" label="付款状态" prop="payApplyState">
        <template slot-scope="scope">
          <div @click="payApplyList(scope.row)">
            <dict-tag style="cursor: pointer;" :options="dict.type.buy_pay_status" :value="scope.row.payApplyState"/>
          </div>
        </template>
      </el-table-column>
      <el-table-column sortable align="center" label="到货状态" prop="arrivalStatus">
        <template slot-scope="scope">
          <div @click="arriveList(scope.row)">
            <dict-tag style="cursor: pointer;" :options="dict.type.daohuo_status" :value="scope.row.arrivalStatus"/>
          </div>
        </template>
      </el-table-column>
      <el-table-column label="定金" align="center" prop="dingjin"/>
      <el-table-column sortable label="已付款" align="center" prop="fukuan"/>
      <el-table-column sortable label="剩余欠款" align="center" prop="shengyu"/>
      <el-table-column sortable label="制单人" align="center" prop="createBy"/>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="260" fixed="right">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-money"
                     v-if="scope.row.payApplyState === '0' || scope.row.payApplyState === '-1' || scope.row.payApplyState === '-3'"
                     @click="payApplyIron(scope.row)" v-hasPermi="['develop:LvBuy:edit']">申请付款
          </el-button>
          <el-button size="mini" type="text" icon="el-icon-edit"
                     v-if="(scope.row.payApplyState === '0' || scope.row.payApplyState === '-1' || scope.row.payApplyState === '-3' )"
                     @click="handleUpdate(scope.row)" v-hasPermi="['develop:LvBuy:edit']">修改
          </el-button>

          <el-popconfirm :title="`确认删除当前数据项？`" @confirm="handleDeleteCgd(scope.row)"
                         v-if="(scope.row.payApplyState === '0' || scope.row.payApplyState === '-1' || scope.row.payApplyState === '-3' ) "
                         style="margin:0 10px;">
            <el-button slot="reference" size="mini" type="text" icon="el-icon-delete"
                       v-hasPermi="['develop:LvBuy:remove']">删除
            </el-button>
          </el-popconfirm>
        </template>
      </el-table-column>
    </el-table>
    <pagination v-show="total>0" :total="total" :page.sync="queryParams.pageNum"
                :limit.sync="queryParams.pageSize" @pagination="getList"/>
    <pay-apply-list-module ref="payApplyListModule"></pay-apply-list-module>
    <pay-apply-iron-form ref="payApplyIronForm" @ok="getList"></pay-apply-iron-form>
    <lv-buy-iron-form ref="buyIronForm" @ok="getList"></lv-buy-iron-form>
    <lv-buy-iron-merge-list-form ref="lvBuyIronMergeListForm" @ok="getList" ></lv-buy-iron-merge-list-form>

  </div>
</template>

<script>
import {accSub, deleteAction, getAction} from '@/api/manage'
import {list} from "@/api/develop/lvBuy";
import PayApplyListModule from "@/views/develop/payApply/module/PayApplyListModule.vue";
import PayApplyIronForm from "@/views/develop/payApply/module/PayApplyIronForm.vue";
import LvBuyIronForm from "@/views/develop/lvBuy/module/LvBuyIronForm.vue";
import LvBuyIronMergeListForm from "@/views/develop/lvBuy/module/LvBuyIronMergeListForm.vue";

export default {
  name: "LvBuyList",
  dicts: ["goods_type", "caigou_type", "pay_material_status", "daohuo_status", "pay_hardware_status", "fp_state", "buy_pay_status"],
  components: {
    LvBuyIronMergeListForm,
    LvBuyIronForm,
    PayApplyIronForm,
    PayApplyListModule,
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 采购单表格数据
      dataList: [],
      dateRange: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        code: null,
        name: null,
        mainId: null,
        nos: null,
        ddDate: null,
        subDate: null,
        type: null,
        ywType: "9",
        tax: null,
        customerId: null,
        customerName: null,
        isBuy: null,
        money: null,
        dingjin: null,
        shengyu: null,
        shuie: null,
        counts: null,
        dunwei: null,
        supplierId: null,
        supplierName: null,
        deptId: null,
        deptName: null,
        userId: null,
        userName: null,
        state: null,
        createBy: this.$store.getters.name,
        orderByColumn: 'a.nos',
        isAsc: "desc"
      },
      url: {
        list: '/lvBuy/lvBuy/listScope',
        removeIronBuy: '/lvBuy/lvBuy/removeIronBuy/',
      },
      tableMaxHeight: window.innerHeight - 320,
    };
  },
  created() {
    this.getList();
    window.addEventListener('resize', this.getTableHeight);
  },
  destroyed() {
    window.removeEventListener('resize', this.getTableHeight)
  },
  methods: {
    /** 排序触发事件 */
    handleSortChange(column, prop, order) {
      this.queryParams.orderByColumn = column.prop;
      this.queryParams.isAsc = column.order;
      this.handleQuery();
    },
    /** 查询铁附件订单拆解明细列表 */
    getList() {
      this.loading = true;
      this.addDateRange(this.queryParams, this.dateRange);
      getAction(this.url.list, this.queryParams).then(response => {
        this.dataList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },

    getTableHeight() {
      this.tableHeight = window.innerHeight - 320
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.dateRange = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },

    handleDeleteCgd(row) {
      const ids = row.id || this.ids;
      deleteAction(this.url.removeIronBuy, ids).then(response => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      deleteAction(this.url.remove, ids).then(response => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      });
    },

    payApplyIron(row) {
      this.$refs.payApplyIronForm.show(row);
    },

    payApplyList(row) {
      this.$refs.payApplyListModule.showByBuy(row.id);
    },

    handleUpdate(row) {
      this.$refs.buyIronForm.handleUpdate(row);
    },

    handleAddMerge(){
      this.$refs.lvBuyIronMergeListForm.show();
    }

  }
};
</script>
